<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Keu_penerimaan_kas_bank extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('keu_penerimaan_kas_bank_model', 'GLBankTrans');
		$this->load->model('keu_kas_bank_detail_model', 'GLBankTransDetail');
		//$this->load->model('fnbankaccount_model', 'BankAccount');
		$this->load->model('combo_model', 'Combo');
		$this->load->model('keu_store_procedure_model', 'SP');
		
		
		
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Penerimaan Kas & Bank";
		$data['data'] = null;
		$data['data']['date_from'] = date('01/m/Y');
		$data['data']['date_to'] = date('t/m/Y');
		viewByTemplate('keu_penerimaan_kas_bank/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->GLBankTrans->GetJSON();
	}
	
	function getdetaildata()
	{
		$this->secure_session();
		echo $this->GLBankTrans->GetDetailJSON(post('id_dok'));
	}
	
		
	function create()
	{
		$data['title'] = " Tambah Penerimaan Kas & Bank";
		$data['data'] = NULL;
		
		$data['kantor_data']		= json_encode($this->Combo->SWOffice());
		$data['cost_center_data']	= json_encode($this->Combo->SWCostCenter());
		$data['valuta_data']		= json_encode($this->Combo->SWValuta());
		
		$data['mode'] = "create";
		viewByTemplate('keu_penerimaan_kas_bank/form', $data, 'form');
	}

	function edit()
	{
		$data['title'] = "Ubah Penerimaan Kas & Bank";
		$data['data'] = $this->GLBankTrans->GetSingleView(get('id_dok'));
				
		$data['kantor_data']		= json_encode($this->Combo->SWOffice());
		$data['cost_center_data']	= json_encode($this->Combo->SWCostCenter());
		$data['valuta_data']		= json_encode($this->Combo->SWValuta());
		
		$data['mode'] = "edit";
		viewByTemplate('keu_penerimaan_kas_bank/form', $data, 'form');
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");

		
		// Validation
		if (post("kd_kantor") == '') {
			$error->Add("kd_kantor", "Kantor", $tpl_empty);
		}
		
		if (post("kd_cost_center") == '') {
			$error->Add("kd_cost_center", "Cost Center", $tpl_empty);
		}
		
		
		if (post("tgl_transaksi") == '') {
			$error->Add("tgl_transaksi", "Tanggal Transaksi", $tpl_empty);
		}
		
		
		if (post("id_coa") == '') {
			$error->Add("id_coa", "Setor Ke-", $tpl_empty);
		}
		
		if (post("kd_valuta") == '') {
			$error->Add("kd_valuta", "Mata Uang", $tpl_empty);
		}
		
		if (toNumber(post("jml_dana")) <= 0) {
			$error->Add("jml_dana", "Jumlah Nilai", $tpl_empty);
		}
		
		
		if (post("keterangan") == '') {
			$error->Add("keterangan", "Keterangan", $tpl_empty);
		}
		
		
		
		return $error;
	}
	
	function dtl_account_data()
	{
		echo $this->GLBankTransDetail->GetJSON();
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('id_dok','kd_kantor', 'tgl_transaksi', 'keterangan', 
											'jml_dana',  'id_coa', 'kurs', 'kd_valuta', 'kd_cost_center', 'kd_bank'));
				
				$data['kurs']		= toNumber($data['kurs']);
				$data['jml_dana']	= toNumber(post('jml_dana'));
					
				if (post("mode") == "create")
				{
					$suffix 	= "/KB/".date_format3(post('tgl_transaksi'), 'MM').'/'.date_format3(post('tgl_transaksi'), 'YYYY');
					$id_dok		= $this->get_data("SELECT (NVL((SELECT MAX(SUBSTR(ID_DOK,0,LENGTH(ID_DOK)-LENGTH('".$suffix."'))) FROM KEU_TRANS_KAS_BANK WHERE SUBSTR(ID_DOK,-1*LENGTH('".$suffix."'),LENGTH('".$suffix."')) = '".$suffix."'),0)+1) CODE FROM DUAL");
					$id_dok		= str_pad($id_dok, 2, '0', STR_PAD_LEFT).$suffix;
				
									
					$data['id_dok']			= $id_dok;
					$data['tgl_transaksi']		= date_format3(post('tgl_transaksi'), 'YYYY-MM-DD');
					
					
					$data['status_transaksi']	= 'N';					
					$data['db_cr']	= 'D';
					$data['create_date']	= NOW;
					$data['create_user']	= USERID;

					$this->GLBankTrans->Add($data);

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.role_id ?? ""));

				}
				else if (post("mode") == "edit")
				{
				
					$data['tgl_transaksi']		= date_format3(post('tgl_transaksi'), 'YYYY-MM-DD');
					$data['modify_date']	= NOW;
					$data['modify_user']	= USERID;
					
					$this->GLBankTrans->Update($data, 
										array('id_dok'	=> $data['id_dok']));

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS002", "edit/" + (data.role_id ?? ""));

				}
				
				
				// Detail Akun
				$dtl_account	= $this->data['dtl_account'];
				$total_deleted	= $dtl_account['num_row']['deleted'];
				$total_inserted	= $dtl_account['num_row']['inserted'];
				$total_updated	= $dtl_account['num_row']['updated'];
				for($i=0,$max_i=$total_deleted; $i<$max_i; $i++){
					$row = $dtl_account['deleted'][$i];
					$this->GLBankTransDetail->DeleteSingle($row['id_dok'], post('kd_cost_center'), $row['id_coa']);
				}
				for($i=0,$max_i=$total_inserted; $i<$max_i; $i++){
					$row = $dtl_account['inserted'][$i];
					
					unset($row['nama_coa']);
					
					
					
					$row['kd_cost_center'] 		= post('kd_cost_center');
					
					$row['kredit']				= toNumber($row['kredit']);
					$row['kredit_c']			= $data['kurs']*$row['kredit'];
					$row['kurs']				= $data['kurs'];
					$row['debet']				= toNumber($row['debet']);
					$row['debet_c']				= $data['kurs']*$row['debet'];
					
					$row['id_dok'] 				= $data['id_dok'];
					$row['create_date']		= NOW;
					$row['create_user']		= USERID;
					
					$this->GLBankTransDetail->Add($row);
				}
				for($i=0,$max_i=$total_updated; $i<$max_i; $i++){
					$row = $dtl_account['updated'][$i];
					
					
					unset($row['nama_coa']);
					
					$row['kd_cost_center'] 	= post('kd_cost_center');
					$row['id_dok'] 			= $data['id_dok'];
					$row['kredit']			= toNumber($row['kredit']);
					$row['kurs']			= $data['kurs'];
					$row['kredit_c']		= $data['kurs']*$row['kredit'];
					
					$row['debet']			= toNumber($row['debet']);
					$row['debet_c']			= $data['kurs']*$row['debet'];
					$row['modify_date']		= NOW;
					$row['modify_user']		= USERID;
				
					$this->GLBankTransDetail->Update($row, array(	'id_dok'			=> $row['id_dok'], 
																	'kd_cost_center'	=> $row['kd_cost_center'],
																	'id_coa'			=> $row['id_coa'] ));
				}
				
				$this->SP->SP_GL_KAS_BANK($data['id_dok'], 'JKB01');
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$id_dok	= get('id_dok');
		
		if ($id_dok != "")
		{
			// Delete Single
			$ls_result = $this->GLBankTrans->DeleteSingle($id_dok);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				$ls_result .= $this->GLBankTrans->DeleteSingle($row['id_dok']);
			}
		}
		
		echo $ls_result;
	}
	
	function print_bukti(){
		$as_id_dok	= get('id_dok');
		
		if($as_id_dok != ''){
			
			// Load FPDF
			$this->load->helper('fpdf');
			
			$this->load->library('Terbilang');
			$Terbilang = new Terbilang();
			
			// Set Data
			$result	= $this->db->query("SELECT	*,
												(SELECT x.currency_name FROM sw_currency x WHERE x.`code`=gl_bank_trans.kd_valuta) currency_name,
												(SELECT x.symbol FROM sw_currency x WHERE x.`code`=gl_bank_trans.kd_valuta) symbol,
												(SELECT x.code_jurnal_type FROM gl_trans x WHERE x.reff_id = gl_bank_trans.id_dok) code_jurnal_type
										FROM gl_bank_trans WHERE id_dok = '".get('id_dok')."'");
			if($result->num_rows() == 1){
				$row	= $result->row_array();
			}else{
				$row	= array('jml_dana' => 0, 'currency_name' => '');
			}
			if(count(explode(' ',trim($row['currency_name'])))>1){ $row['currency_name'] = end(explode(' ',trim($row['currency_name']))); }
			$row['terbilang']	= $Terbilang->eja( $row['jml_dana'] ) . ' ' . $row['currency_name'];
			$data['data']		= $row;
			
			
			$result	= $this->db->query("SELECT	b.id_coa, d.nama_coa,SUM(b.debet_c) debet_c, SUM(kredit_c) kredit_c,SUM(b.debet) debet, SUM(kredit) kredit
										FROM	gl_trans a
												JOIN gl_trans_detail b ON a.id_dok = b.id_dok
												JOIN gl_bank_trans c ON c.id_dok = a.reff_id
												JOIN gl_coa d ON b.id_coa = d.`code`
										WHERE	c.id_dok='".$as_id_dok."'
										GROUP BY b.id_coa
										ORDER BY SUM(b.debet_c) desc, b.id_coa ");
			if($result->num_rows() >0){
				$data['arr_detail']	= $result->result_array();
			}else{
				$data['arr_detail']	= array();
			}
			
			// Load View
			$this->load->view('keu_penerimaan_kas_bank/print_bukti_penerimaan', $data);
			
		}
		
		/*
		// Jasper Reports
		$this->load_report(	'gl_bukti_kas.jrxml',
							array(	'id_dok'	=> get('id_dok'),
									'terbilang'	=> $ls_terbilang) );*/
	}
	
}
?>